<?php

namespace App\V3\Api\Controllers;

use App\Helper\Utils\Util;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\V3\Api\Models\HcMaterielClassModel;
use Illuminate\Support\Facades\Validator;
use App\V3\Api\Services\HcMaterielClassService;
use App\Http\Controllers\Api\BaseController;

class HcMaterielClassController extends BaseController
{
    public function list(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        return $this->success(
            HcMaterielClassService::index($this->getMainUserId(), $data)
        );
    }

    /**
     *商品画册下拉列表
     */
    public function select()
    {
        return $this->success(
            HcMaterielClassService::select($this->getMainUserId())
        );
    }

    /**
     * 新建商品画册分类
     * @param Request $request
     */
    public function add(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            //type:0->一级分类;1->二级分类
            'type' => [
                'required',
                Rule::In([0, 1]),
            ],//页面大小
            'class_name' => 'required',//分类名称
            'class_no' => 'required',//分类编号
            'parent_id' => 'required|integer',//上级分类id
        ],
            [
                'type.required' => '请选择类型',
                'type.in' => '类型不合法，请重新选择',
                'class_name.required' => '请输入分类名称',
                'class_no.required' => '请输入分类编号',
//                'class_no.size' => '编号只能由四位的字母或数字组成',
                'parent_id.required' => '请选择上一级分类',
                'parent_id.integer' => '请输入分类编号',
            ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $data['parent_id'] = !$data['type']?0:$data['parent_id'];
        $data['created_at'] = time();
        $data['main_id'] = $this->getMainUserId();

        $result = HcMaterielClassService::add($this->getMainUserId(), $data);

        if(!$result['code']) {
            $this->log('新建商品画册分类', $data);
            return $this->success(['id' => $result['data']]);
        }
        else {
            return $this->fail('', $result['msg']);
        }
    }

    /**
     * 删除指定商品分类
     */
    public function del(Request $request)
    {
        if(empty($request->id)) {
            return $this->fail('', '请选择要删除的商品分类id');
        }

        //是否存在该指定商品分类
        $checkClass = HcMaterielClassService::checkClass($request->id);
        if(!$checkClass) {
            return $this->fail('', '您要删除的分类不存在，请重试');
        }

        $checkNext = HcMaterielClassService::checkNext($request->id);

        switch($checkNext) {
            case 1:
                return $this->fail('', '若需要删除该一级分类，请先删除其所有的二级分类');
            case 2:
                return $this->fail('', '若需要删除该二级分类，请先删除其所有的画册商品');
        }

        HcMaterielClassModel::where('id', $request->id)->delete();
        $this->log('删除商品分类', $request->id);
        return $this->success();
    }

    /**
     * 编辑商品分类信息
     */
    public function edit(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' => 'required|integer',
            //type:0->一级分类;1->二级分类
            'type' => [
                'required',
                Rule::In([0, 1]),
            ],//页面大小
            'class_name' => 'required',//分类名称
            'class_no' => 'required',//分类编号
            'parent_id' => 'required|integer',//上级分类id
        ],
            [
                'id.required' => '分类id标识丢失',
                'id.integer' => '分类id类型错误',
                'type.in' => '类型不合法，请重新选择',
                'class_name.required' => '请输入分类名称',
                'class_no.required' => '请输入分类编号',
//                'class_no.size' => '编号只能由四位的字母或数字组成',
                'parent_id.required' => '上一级分类id标识丢失',
            ]
        );

        //参数错误响应
        if($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $data['parent_id'] = !$data['type']?0:$data['parent_id'];
        $data['created_at'] = time();
        $data['main_id'] = $this->getMainUserId();

        $result = HcMaterielClassService::edit($data);

        switch($result) {
            case 0:
                return $this->fail('', '目标商品分类不存在，请重试');
            case 1:
                return $this->fail('', '操作的数据不合法，请重试');
            case 2:
                return $this->fail('', '请选择一级商品分类');
            case 3:
                return $this->fail('', '分类编号`'.$data['class_no'].'`已存在');
            case 4:
                return $this->fail('', '分类`'.$data['class_name'].'`已存在');
            default:
                $this->log('修改分类信息', $data);
                return $this->success();
        }
    }

    //校验物料分类随机code
    public function randNo()
    {
        $data['class_no'] = Util::randStr(4);

        while(!empty(HcMaterielClassModel::checkHcMaterielClassCode($data['class_no'], $this->getMainUserId()))) {
            $data['class_no'] = Util::randStr(4);
        }

        return $this->success($data);
    }
}
